package com.inmobi.re.container.mraidimpl;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.webkit.URLUtil;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import com.inmobi.commons.internal.Log;
import com.inmobi.re.container.IMWebView;
import com.inmobi.re.container.WrapperFunctions;
import com.inmobi.re.controller.JSController.Dimensions;
import com.inmobi.re.controller.JSController.ExpandProperties;
import com.inmobi.re.controller.JSController.PlayerProperties;
import com.inmobi.re.controller.util.AVPlayer;
import com.inmobi.re.controller.util.AVPlayer.playerState;
import com.inmobi.re.controller.util.AVPlayerListener;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

public class MRAIDAudioVideoController
{
  private IMWebView a;
  public Hashtable<String, AVPlayer> audioPlayerList = new Hashtable();
  public AVPlayer audioplayer;
  public AVPlayer videoPlayer;
  public Hashtable<String, AVPlayer> videoPlayerList = new Hashtable();
  public int videoValidateWidth;

  public MRAIDAudioVideoController(IMWebView paramIMWebView)
  {
    this.a = paramIMWebView;
  }

  private AVPlayer a(String paramString)
  {
    if (!this.videoPlayerList.isEmpty())
      return (AVPlayer)this.videoPlayerList.get(paramString);
    return null;
  }

  private void a(AVPlayer paramAVPlayer, JSController.Dimensions paramDimensions)
  {
    int i = (int)(-99999.0F * this.a.getDensity());
    FrameLayout.LayoutParams localLayoutParams1;
    FrameLayout.LayoutParams localLayoutParams2;
    if (paramAVPlayer.isInlineVideo())
    {
      localLayoutParams1 = new FrameLayout.LayoutParams(paramDimensions.width, paramDimensions.height);
      localLayoutParams2 = (FrameLayout.LayoutParams)paramAVPlayer.getLayoutParams();
      if ((paramDimensions.x != i) || (paramDimensions.y != i))
        break label94;
      localLayoutParams1.leftMargin = localLayoutParams2.leftMargin;
    }
    for (localLayoutParams1.topMargin = localLayoutParams2.topMargin; ; localLayoutParams1.topMargin = paramDimensions.y)
    {
      localLayoutParams1.gravity = 3;
      paramAVPlayer.setLayoutParams(localLayoutParams1);
      return;
      label94: localLayoutParams1.leftMargin = paramDimensions.x;
    }
  }

  private boolean a(String paramString1, String paramString2, Activity paramActivity)
  {
    if (!this.audioPlayerList.isEmpty())
    {
      this.audioplayer = ((AVPlayer)this.audioPlayerList.get(paramString1));
      if (this.audioplayer == null)
      {
        if (this.audioPlayerList.size() > 4)
        {
          this.a.raiseError("Too many audio players", "playAudio");
          return false;
        }
        this.audioplayer = new AVPlayer(paramActivity, this.a);
      }
    }
    while (true)
    {
      return true;
      if ((this.audioplayer.getMediaURL().equals(paramString2)) || (paramString2.length() == 0))
      {
        if (this.audioplayer.getState() == AVPlayer.playerState.PLAYING)
          return false;
        if (this.audioplayer.getState() == AVPlayer.playerState.INIT)
        {
          if (this.audioplayer.isPrepared())
            this.audioplayer.start();
          while (true)
          {
            return false;
            this.audioplayer.setAutoPlay(true);
          }
        }
        if (this.audioplayer.getState() == AVPlayer.playerState.PAUSED)
        {
          this.audioplayer.start();
          return false;
        }
        JSController.PlayerProperties localPlayerProperties = this.audioplayer.getProperties();
        String str = this.audioplayer.getMediaURL();
        this.audioplayer.releasePlayer(false);
        this.audioPlayerList.remove(paramString1);
        this.audioplayer = new AVPlayer(paramActivity, this.a);
        this.audioplayer.setPlayData(localPlayerProperties, str);
      }
      else
      {
        this.audioplayer.releasePlayer(false);
        this.audioPlayerList.remove(paramString1);
        this.audioplayer = new AVPlayer(paramActivity, this.a);
        continue;
        this.audioplayer = new AVPlayer(paramActivity, this.a);
      }
    }
  }

  private boolean a(String paramString1, String paramString2, Activity paramActivity, JSController.Dimensions paramDimensions)
  {
    if ((this.videoPlayer == null) || (!paramString1.equalsIgnoreCase(this.videoPlayer.getPropertyID())))
      return b(paramString1, paramString2, paramActivity);
    AVPlayer.playerState localplayerState = this.videoPlayer.getState();
    if (paramString1.equalsIgnoreCase(this.videoPlayer.getPropertyID()))
    {
      String str = this.videoPlayer.getMediaURL();
      if ((paramString2.length() == 0) || (paramString2.equalsIgnoreCase(str)))
      {
        switch (e.a[localplayerState.ordinal()])
        {
        default:
          return false;
        case 1:
          this.videoPlayer.start();
          a(this.videoPlayer, paramDimensions);
          return false;
        case 2:
          a(this.videoPlayer, paramDimensions);
          return false;
        case 3:
          if (!this.videoPlayer.getProperties().doLoop())
            this.videoPlayer.start();
          a(this.videoPlayer, paramDimensions);
          return false;
        case 4:
        }
        if (this.videoPlayer.isPrepared())
          this.videoPlayer.start();
        while (true)
        {
          a(this.videoPlayer, paramDimensions);
          return false;
          this.videoPlayer.setAutoPlay(true);
        }
      }
      if (!URLUtil.isValidUrl(paramString2))
      {
        this.a.raiseError("Request must specify a valid URL", "playVideo");
        return false;
      }
      this.videoPlayer.releasePlayer(false);
      this.videoPlayer = new AVPlayer(paramActivity, this.a);
    }
    return true;
  }

  private boolean a(Hashtable<String, AVPlayer> paramHashtable, AVPlayer paramAVPlayer)
  {
    Iterator localIterator = paramHashtable.entrySet().iterator();
    Map.Entry localEntry;
    do
    {
      if (!localIterator.hasNext())
        break;
      localEntry = (Map.Entry)localIterator.next();
    }
    while (localEntry.getValue() != paramAVPlayer);
    for (String str = (String)localEntry.getKey(); ; str = null)
    {
      if (str != null)
      {
        this.audioPlayerList.remove(str);
        return true;
      }
      return false;
    }
  }

  private boolean b(String paramString1, String paramString2, Activity paramActivity)
  {
    if (((paramString2.length() != 0) && (!URLUtil.isValidUrl(paramString2))) || ((paramString2.length() == 0) && (!this.videoPlayerList.containsKey(paramString1))))
    {
      this.a.raiseError("Request must specify a valid URL", "playVideo");
      return false;
    }
    if (this.videoPlayer != null)
    {
      this.videoPlayer.hide();
      this.videoPlayerList.put(this.videoPlayer.getPropertyID(), this.videoPlayer);
    }
    AVPlayer localAVPlayer = a(paramString1);
    if (localAVPlayer == null);
    for (this.videoPlayer = new AVPlayer(paramActivity, this.a); ; this.videoPlayer = localAVPlayer)
    {
      if (paramString2.length() == 0)
      {
        this.videoPlayer.setPlayData(localAVPlayer.getProperties(), localAVPlayer.getMediaURL());
        this.videoPlayer.setPlayDimensions(localAVPlayer.getPlayDimensions());
      }
      this.videoPlayerList.remove(paramString1);
      return true;
    }
  }

  public AVPlayer getCurrentAudioPlayer(String paramString)
  {
    try
    {
      AVPlayer localAVPlayer;
      if ((this.audioplayer != null) && (this.audioplayer.getPropertyID().equalsIgnoreCase(paramString)))
        localAVPlayer = this.audioplayer;
      while (true)
      {
        return localAVPlayer;
        boolean bool1 = this.audioPlayerList.isEmpty();
        localAVPlayer = null;
        if (!bool1)
        {
          boolean bool2 = this.audioPlayerList.containsKey(paramString);
          localAVPlayer = null;
          if (bool2)
            localAVPlayer = (AVPlayer)this.audioPlayerList.get(paramString);
        }
      }
    }
    finally
    {
    }
  }

  public AVPlayer getVideoPlayer(String paramString)
  {
    try
    {
      AVPlayer localAVPlayer;
      if ((this.videoPlayer != null) && (this.videoPlayer.getPropertyID().equalsIgnoreCase(paramString)))
        localAVPlayer = this.videoPlayer;
      while (true)
      {
        return localAVPlayer;
        boolean bool1 = this.videoPlayerList.isEmpty();
        localAVPlayer = null;
        if (!bool1)
        {
          boolean bool2 = this.videoPlayerList.containsKey(paramString);
          localAVPlayer = null;
          if (bool2)
            localAVPlayer = (AVPlayer)this.videoPlayerList.get(paramString);
        }
      }
    }
    finally
    {
    }
  }

  public void hidePlayers()
  {
    Log.debug("IMRE_3.7.0", "MRAIDAudioVideoController: hiding all players");
    if ((this.videoPlayer != null) && (this.videoPlayer.getState() != AVPlayer.playerState.RELEASED))
    {
      this.videoPlayerList.put(this.videoPlayer.getPropertyID(), this.videoPlayer);
      this.videoPlayer.hide();
    }
    Iterator localIterator = this.audioPlayerList.entrySet().iterator();
    while (localIterator.hasNext())
    {
      Map.Entry localEntry = (Map.Entry)localIterator.next();
      AVPlayer localAVPlayer = (AVPlayer)localEntry.getValue();
      switch (e.a[localAVPlayer.getState().ordinal()])
      {
      case 3:
      default:
        break;
      case 2:
        localAVPlayer.pause();
        break;
      case 4:
        localAVPlayer.releasePlayer(false);
        this.audioPlayerList.remove(localEntry.getKey());
      }
    }
  }

  public void mediaPlayerReleased(AVPlayer paramAVPlayer)
  {
    if (paramAVPlayer == this.audioplayer)
      this.audioplayer = null;
    if (paramAVPlayer == this.videoPlayer)
      this.videoPlayer = null;
    if (a(this.audioPlayerList, paramAVPlayer))
      return;
    a(this.videoPlayerList, paramAVPlayer);
  }

  public void playAudioImpl(Bundle paramBundle, Activity paramActivity)
  {
    final JSController.PlayerProperties localPlayerProperties = (JSController.PlayerProperties)paramBundle.getParcelable("player_properties");
    String str = paramBundle.getString("expand_url");
    if (str == null)
      str = "";
    if (!a(localPlayerProperties.id, str, paramActivity));
    do
    {
      return;
      if (((str.length() != 0) && (!URLUtil.isValidUrl(str))) || ((str.length() == 0) && (!this.audioPlayerList.containsKey(localPlayerProperties.id))))
      {
        this.a.raiseError("Request must specify a valid URL", "playAudio");
        return;
      }
    }
    while (this.audioplayer == null);
    if (str.length() != 0)
      this.audioplayer.setPlayData(localPlayerProperties, str);
    this.audioPlayerList.put(localPlayerProperties.id, this.audioplayer);
    FrameLayout localFrameLayout = (FrameLayout)paramActivity.findViewById(16908290);
    if (localPlayerProperties.isFullScreen())
    {
      RelativeLayout.LayoutParams localLayoutParams = new RelativeLayout.LayoutParams(WrapperFunctions.getParamFillParent(), WrapperFunctions.getParamFillParent());
      localLayoutParams.addRule(13);
      this.audioplayer.setLayoutParams(localLayoutParams);
      RelativeLayout localRelativeLayout = new RelativeLayout(paramActivity);
      localRelativeLayout.setOnTouchListener(new g());
      localRelativeLayout.setBackgroundColor(-16777216);
      localFrameLayout.addView(localRelativeLayout, new RelativeLayout.LayoutParams(WrapperFunctions.getParamFillParent(), WrapperFunctions.getParamFillParent()));
      localRelativeLayout.addView(this.audioplayer);
      this.audioplayer.setBackGroundLayout(localRelativeLayout);
      this.audioplayer.requestFocus();
      this.audioplayer.setOnKeyListener(new f());
    }
    while (true)
    {
      this.audioplayer.setListener(new b(localPlayerProperties));
      this.audioplayer.play();
      return;
      this.audioplayer.setLayoutParams(new ViewGroup.LayoutParams(1, 1));
      localFrameLayout.addView(this.audioplayer);
    }
  }

  public void playVideoImpl(Bundle paramBundle, Activity paramActivity)
  {
    JSController.PlayerProperties localPlayerProperties1 = (JSController.PlayerProperties)paramBundle.getParcelable("player_properties");
    JSController.Dimensions localDimensions = (JSController.Dimensions)paramBundle.getParcelable("expand_dimensions");
    Log.debug("IMRE_3.7.0", "Final dimensions: " + localDimensions);
    String str = paramBundle.getString("expand_url");
    if (!a(localPlayerProperties1.id, str, paramActivity, localDimensions))
      return;
    this.a.setBusy(true);
    JSController.PlayerProperties localPlayerProperties3;
    if (str.length() == 0)
    {
      localPlayerProperties3 = this.videoPlayer.getProperties();
      localDimensions = this.videoPlayer.getPlayDimensions();
      this.videoPlayer.getMediaURL();
    }
    for (JSController.PlayerProperties localPlayerProperties2 = localPlayerProperties3; this.videoPlayer.getState() == AVPlayer.playerState.HIDDEN; localPlayerProperties2 = localPlayerProperties1)
    {
      this.videoPlayer.pseudoPause = true;
      this.videoPlayer.show();
      return;
      this.videoPlayer.setPlayData(localPlayerProperties1, str);
      this.videoPlayer.setPlayDimensions(localDimensions);
    }
    FrameLayout localFrameLayout1 = (FrameLayout)paramActivity.findViewById(16908290);
    if (localPlayerProperties2.isFullScreen())
    {
      RelativeLayout.LayoutParams localLayoutParams = new RelativeLayout.LayoutParams(WrapperFunctions.getParamFillParent(), WrapperFunctions.getParamFillParent());
      localLayoutParams.addRule(13);
      this.videoPlayer.setLayoutParams(localLayoutParams);
      RelativeLayout localRelativeLayout = new RelativeLayout(paramActivity);
      localRelativeLayout.setOnTouchListener(new a());
      localRelativeLayout.setBackgroundColor(-16777216);
      localFrameLayout1.addView(localRelativeLayout, new FrameLayout.LayoutParams(WrapperFunctions.getParamFillParent(), WrapperFunctions.getParamFillParent()));
      localRelativeLayout.addView(this.videoPlayer);
      this.videoPlayer.setBackGroundLayout(localRelativeLayout);
      this.videoPlayer.requestFocus();
      this.videoPlayer.setOnKeyListener(new d());
      this.videoPlayer.setListener(new c());
      this.videoPlayer.play();
      return;
    }
    FrameLayout.LayoutParams localLayoutParams1 = new FrameLayout.LayoutParams(localDimensions.width, localDimensions.height);
    FrameLayout localFrameLayout2 = new FrameLayout(paramActivity);
    if (this.a.mExpandController.expandProperties == null)
      localLayoutParams1.leftMargin = localDimensions.x;
    for (localLayoutParams1.topMargin = localDimensions.y; ; localLayoutParams1.topMargin = (localDimensions.y + this.a.mExpandController.expandProperties.currentY))
    {
      localLayoutParams1.gravity = 3;
      this.videoPlayer.setLayoutParams(localLayoutParams1);
      localFrameLayout1.addView(localFrameLayout2, new FrameLayout.LayoutParams(WrapperFunctions.getParamFillParent(), WrapperFunctions.getParamFillParent()));
      this.videoPlayer.setBackGroundLayout(localFrameLayout2);
      localFrameLayout2.addView(this.videoPlayer);
      break;
      localLayoutParams1.leftMargin = (localDimensions.x + this.a.mExpandController.expandProperties.currentX);
    }
  }

  public void releaseAllPlayers()
  {
    if (this.videoPlayer != null)
      this.videoPlayerList.put(this.videoPlayer.getPropertyID(), this.videoPlayer);
    try
    {
      Object[] arrayOfObject2 = this.videoPlayerList.values().toArray();
      int k = arrayOfObject2.length;
      int m = 0;
      while (true)
        if (m < k)
        {
          Object localObject2 = arrayOfObject2[m];
          try
          {
            ((AVPlayer)localObject2).releasePlayer(IMWebView.userInitiatedClose);
            m++;
          }
          catch (Exception localException4)
          {
            while (true)
              Log.internal("IMRE_3.7.0", "Unable to release player");
          }
        }
    }
    catch (Exception localException1)
    {
      Log.internal("IMRE_3.7.0", "IMwebview release all players ", localException1);
      this.videoPlayerList.clear();
      this.videoPlayer = null;
      try
      {
        Object[] arrayOfObject1 = this.audioPlayerList.values().toArray();
        int i = arrayOfObject1.length;
        int j = 0;
        while (true)
          if (j < i)
          {
            Object localObject1 = arrayOfObject1[j];
            try
            {
              ((AVPlayer)localObject1).releasePlayer(IMWebView.userInitiatedClose);
              j++;
            }
            catch (Exception localException3)
            {
              while (true)
                Log.internal("IMRE_3.7.0", "Unable to release player");
            }
          }
      }
      catch (Exception localException2)
      {
        Log.internal("IMRE_3.7.0", "IMwebview release all players ", localException2);
        IMWebView.userInitiatedClose = false;
        this.audioPlayerList.clear();
        this.audioplayer = null;
      }
    }
  }

  public void validateVideoDimensions(JSController.Dimensions paramDimensions)
  {
    paramDimensions.width = ((int)(paramDimensions.width * this.a.getDensity()));
    paramDimensions.height = ((int)(paramDimensions.height * this.a.getDensity()));
    paramDimensions.x = ((int)(paramDimensions.x * this.a.getDensity()));
    paramDimensions.y = ((int)(paramDimensions.y * this.a.getDensity()));
  }

  class a
    implements View.OnTouchListener
  {
    a()
    {
    }

    public boolean onTouch(View paramView, MotionEvent paramMotionEvent)
    {
      return true;
    }
  }

  class b
    implements AVPlayerListener
  {
    b(JSController.PlayerProperties arg2)
    {
    }

    public void onComplete(AVPlayer paramAVPlayer)
    {
      try
      {
        if (localPlayerProperties.isFullScreen())
        {
          ViewGroup localViewGroup2 = (ViewGroup)paramAVPlayer.getBackGroundLayout().getParent();
          if (localViewGroup2 != null)
            localViewGroup2.removeView(paramAVPlayer.getBackGroundLayout());
        }
        else
        {
          ViewGroup localViewGroup1 = (ViewGroup)paramAVPlayer.getParent();
          if (localViewGroup1 != null)
          {
            localViewGroup1.removeView(paramAVPlayer);
            return;
          }
        }
      }
      catch (Exception localException)
      {
        Log.debug("IMRE_3.7.0", "Problem removing the audio relativelayout", localException);
      }
    }

    public void onError(AVPlayer paramAVPlayer)
    {
      onComplete(paramAVPlayer);
    }

    public void onPrepared(AVPlayer paramAVPlayer)
    {
    }
  }

  class c
    implements AVPlayerListener
  {
    c()
    {
    }

    public void onComplete(AVPlayer paramAVPlayer)
    {
      MRAIDAudioVideoController.a(MRAIDAudioVideoController.this).setBusy(false);
      try
      {
        ViewGroup localViewGroup = paramAVPlayer.getBackGroundLayout();
        if (localViewGroup != null)
          ((ViewGroup)localViewGroup.getParent()).removeView(paramAVPlayer.getBackGroundLayout());
        paramAVPlayer.setBackGroundLayout(null);
      }
      catch (Exception localException)
      {
        try
        {
          while (true)
          {
            if ((MRAIDAudioVideoController.this.videoPlayer != null) && (paramAVPlayer.getPropertyID().equalsIgnoreCase(MRAIDAudioVideoController.this.videoPlayer.getPropertyID())))
              MRAIDAudioVideoController.this.videoPlayer = null;
            return;
            localException = localException;
            Log.debug("IMRE_3.7.0", "Problem removing the video framelayout or relativelayout depending on video startstyle", localException);
          }
        }
        finally
        {
        }
      }
    }

    public void onError(AVPlayer paramAVPlayer)
    {
      onComplete(paramAVPlayer);
    }

    public void onPrepared(AVPlayer paramAVPlayer)
    {
    }
  }

  class d
    implements View.OnKeyListener
  {
    d()
    {
    }

    public boolean onKey(View paramView, int paramInt, KeyEvent paramKeyEvent)
    {
      if ((4 == paramKeyEvent.getKeyCode()) && (paramKeyEvent.getAction() == 0))
      {
        Log.debug("IMRE_3.7.0", "Back pressed while fullscreen video is playing");
        MRAIDAudioVideoController.this.videoPlayer.releasePlayer(true);
        return true;
      }
      return false;
    }
  }

  class f
    implements View.OnKeyListener
  {
    f()
    {
    }

    public boolean onKey(View paramView, int paramInt, KeyEvent paramKeyEvent)
    {
      if ((4 == paramKeyEvent.getKeyCode()) && (paramKeyEvent.getAction() == 0))
      {
        Log.debug("IMRE_3.7.0", "Back button pressed while fullscreen audio was playing");
        MRAIDAudioVideoController.this.audioplayer.releasePlayer(true);
        return true;
      }
      return false;
    }
  }

  class g
    implements View.OnTouchListener
  {
    g()
    {
    }

    public boolean onTouch(View paramView, MotionEvent paramMotionEvent)
    {
      return true;
    }
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.inmobi.re.container.mraidimpl.MRAIDAudioVideoController
 * JD-Core Version:    0.6.2
 */